package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.JingfeiPici;
import com.ruoyi.system.domain.ZhuanjiaBiandongDetail;
import com.ruoyi.system.service.IDeptidOlddeptidService;
import com.ruoyi.system.service.IJingfeiPiciService;
import com.ruoyi.system.service.IZhuanjiaBiandongDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ZhuanjiaBiandongQuerenMapper;
import com.ruoyi.system.domain.ZhuanjiaBiandongQueren;
import com.ruoyi.system.service.IZhuanjiaBiandongQuerenService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-10
 */
@Service
public class ZhuanjiaBiandongQuerenServiceImpl implements IZhuanjiaBiandongQuerenService 
{
    @Autowired
    private ZhuanjiaBiandongQuerenMapper zhuanjiaBiandongQuerenMapper;
    @Autowired
    private IDeptidOlddeptidService deptidOlddeptidService;
    @Autowired
    private IZhuanjiaBiandongDetailService  zhuanjiaBiandongDetailService;
    @Autowired
    private IJingfeiPiciService jingfeiPiciService;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public ZhuanjiaBiandongQueren selectZhuanjiaBiandongQuerenById(String id)
    {
        return zhuanjiaBiandongQuerenMapper.selectZhuanjiaBiandongQuerenById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param zhuanjiaBiandongQueren 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<ZhuanjiaBiandongQueren> selectZhuanjiaBiandongQuerenList(ZhuanjiaBiandongQueren zhuanjiaBiandongQueren)
    {
        return zhuanjiaBiandongQuerenMapper.selectZhuanjiaBiandongQuerenList(zhuanjiaBiandongQueren);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param zhuanjiaBiandongQueren 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertZhuanjiaBiandongQueren(ZhuanjiaBiandongQueren zhuanjiaBiandongQueren)
    {
        return zhuanjiaBiandongQuerenMapper.insertZhuanjiaBiandongQueren(zhuanjiaBiandongQueren);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param zhuanjiaBiandongQueren 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateZhuanjiaBiandongQueren(ZhuanjiaBiandongQueren zhuanjiaBiandongQueren)
    {
        return zhuanjiaBiandongQuerenMapper.updateZhuanjiaBiandongQueren(zhuanjiaBiandongQueren);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteZhuanjiaBiandongQuerenByIds(String ids)
    {
        return zhuanjiaBiandongQuerenMapper.deleteZhuanjiaBiandongQuerenByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteZhuanjiaBiandongQuerenById(String id)
    {
        return zhuanjiaBiandongQuerenMapper.deleteZhuanjiaBiandongQuerenById(id);
    }

    @Override
    public List<Map<String,Object>>findQuerenByParentid(String ppDeptId,String ppJingfeipiciId,String ppDeptName,String ppShifouqueren){


        ppDeptId = deptidOlddeptidService.DeptIdToOldDeptId(ppDeptId);
        ppDeptName = "%"+ppDeptName+"%";

        return zhuanjiaBiandongQuerenMapper.findQuerenByParentid(ppDeptId,ppJingfeipiciId,ppDeptName,ppShifouqueren);
    }

    @Override
    @Transactional
    public boolean querenZJZX(ZhuanjiaBiandongDetail ppZhuanjiabiandongDetail,ZhuanjiaBiandongQueren ppZhuanjiabiandongQueren){
        boolean ok = true;

        int addDetail = zhuanjiaBiandongDetailService.insertZhuanjiaBiandongDetail(ppZhuanjiabiandongDetail);
        if (addDetail!=1){
            ok = false;
            throw new RuntimeException("无变动添加失败");
        }
        JingfeiPici mmJingfeipici = new JingfeiPici();
        mmJingfeipici.setIsdangqianpici(1);
        mmJingfeipici.setDeptid("0");
        List<JingfeiPici> mmJingfeipiciList = jingfeiPiciService.selectJingfeiPiciList(mmJingfeipici);
        for (int i=0;i<mmJingfeipiciList.size();i++){
            String mmPiciId = mmJingfeipiciList.get(i).getId();
            ppZhuanjiabiandongQueren.setId(UUID.randomUUID().toString());
            ppZhuanjiabiandongQueren.setJingfeipiciid(mmPiciId);

            ZhuanjiaBiandongQueren tempZhuanjiabiandongQueren = new ZhuanjiaBiandongQueren();
            tempZhuanjiabiandongQueren.setJingfeipiciid(mmPiciId);
            tempZhuanjiabiandongQueren.setDeptid(ppZhuanjiabiandongQueren.getDeptid());
            List<ZhuanjiaBiandongQueren> mmTempList = selectZhuanjiaBiandongQuerenList(tempZhuanjiabiandongQueren);
            if (mmTempList.size()==1){
                ZhuanjiaBiandongQueren one = mmTempList.get(0);
                one.setQuerenshijian(new Date());
                int modifyOne = updateZhuanjiaBiandongQueren(one);
                if (modifyOne!=1){
                    ok = false;
                    throw new RuntimeException("变动确认修改失败");
                }
            } else if (mmTempList.size()>1) {
                ok = false;
                throw new RuntimeException("变动确认查询失败");
            } else{
                int addQueren = insertZhuanjiaBiandongQueren(ppZhuanjiabiandongQueren);
                if (addQueren!=1){
                    ok = false;
                    throw new RuntimeException("变动确认添加失败");
                }
            }

        }

        return ok;
    }
}
